﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using KCL.Code.Data;
using KCL.Code.Business;

namespace KCL.Input
{
    public partial class InputFinishedProduct : System.Web.UI.Page
    {
        public string txtorderId, txtCustomerId, txtCreatedDate, txtDeliveryDate, txtOrderCode;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[SessionManager.InSection] != null)
            {
                if (this.Page.IsPostBack == false)
                {
                    BindUnfinishedOrderID();
                    if (HttpContext.Current.Request.Params["orderId"] == null)
                    {
                        this.Page.Response.Redirect(string.Format("../Input/InputFinishedProduct.aspx?orderId={0}", ddlOrderID.SelectedValue));
                    }
                    else
                    {
                        ddlOrderID.SelectedItem.Selected = false;
                        ddlOrderID.Items.FindByValue(HttpContext.Current.Request.Params["orderId"]).Selected = true;
                    }
                }

                FillOrderInfo(Convert.ToInt64(HttpContext.Current.Request.Params["orderId"]));

                if ((int)Session[SessionManager.InSection] != 1)
                {
                    Response.Redirect("../");
                }
            }
            else
            {
                Response.Redirect("../");
            }
        }

        private void FillOrderInfo(Int64 orderId)
        {
            BaseDAO dao = new BaseDAO();
            using (SqlConnection connection = dao.GetConnection())
            {
                string sqlStatement = string.Format("SELECT [Order].[id],[Order].[code],[deliveryDate], [createdDate], [name], [address] FROM [Order], [Customer] WHERE [isFinished] = 0 AND [Order].[id] = {0} AND [customerId] = [Customer].[id]", orderId);
                SqlCommand command = new SqlCommand(sqlStatement, connection);

                SqlDataAdapter dataAdap = new SqlDataAdapter(command);
                DataTable data = new DataTable();

                try
                {
                    connection.Open();
                    dataAdap.Fill(data);

                    int currentIndex = 0;
                    txtorderId = data.Rows[currentIndex]["id"].ToString();
                    txtCustomerId = string.Format("{0} ({1})",data.Rows[currentIndex]["name"].ToString(),data.Rows[currentIndex]["address"].ToString());
                    txtCreatedDate = data.Rows[currentIndex]["createdDate"].ToString();
                    txtDeliveryDate = data.Rows[currentIndex]["deliveryDate"].ToString();
                    txtOrderCode = data.Rows[currentIndex]["code"].ToString();
                }
                catch
                {
                    txtorderId = string.Empty;
                    txtCustomerId = string.Empty;
                    txtCreatedDate = string.Empty;
                    txtDeliveryDate = string.Empty;
                    txtOrderCode = string.Empty;
                    return;
                }
            }
        }

        private void BindUnfinishedOrderID()
        {
            InputFinishedProductDAO dao = new InputFinishedProductDAO();
            DataTable unfinishedProductID = new DataTable();
            unfinishedProductID = dao.GetUnifinishedProductID();
            ddlOrderID.DataSource = unfinishedProductID;
            ddlOrderID.DataTextField = "id";
            ddlOrderID.DataValueField = "id";            
            ddlOrderID.DataBind();
            ddlOrderID.Items.Add(new ListItem("...", "0"));
            ddlOrderID.Items.FindByText("...").Selected = true;
        }

        protected void ddlOrderID_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Page.Response.Redirect(string.Format("../Input/InputFinishedProduct.aspx?orderId={0}", ddlOrderID.SelectedValue));            
        }         
    }
}